home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / gifsave.zip / example.c next >
C/C++ Source or Header  |  1992-09-26  |  6KB  |  242 lines

  1.  
  2. /**************************************************************************
  3.  *
  4.  *  FILE:           EXAMPLE.C
  5.  *
  6.  *  MODULE OF:      EXAMPLE
  7.  *
  8.  *  DESCRIPTION:    Example program using GIFSAVE.
  9.  *
  10.  *                  Produces output to an EGA-screen, then dumps it to
  11.  *                  a GIF-file.
  12.  *
  13.  *                  This program is rather slow. The bottleneck is
  14.  *                  Borland's getpixel() -function, not the GIFSAVE-
  15.  *                  functions.
  16.  *
  17.  *  WRITTEN BY:     Sverre H. Huseby
  18.  *
  19.  **************************************************************************/
  20.  
  21.  
  22.  
  23. #ifndef __TURBOC__
  24.   #error This program must be compiled using a Borland C compiler
  25. #endif
  26.  
  27.  
  28.  
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <graphics.h>
  32.  
  33. #include "gifsave.h"
  34.  
  35.  
  36.  
  37.  
  38.  
  39. /**************************************************************************
  40.  *                                                                        *
  41.  *                   P R I V A T E    F U N C T I O N S                   *
  42.  *                                                                        *
  43.  **************************************************************************/
  44.  
  45. /*-------------------------------------------------------------------------
  46.  *
  47.  *  NAME:           DrawScreen()
  48.  *
  49.  *  DESCRIPTION:    Produces some output on the graphic screen.
  50.  *
  51.  *  PARAMETERS:     None
  52.  *
  53.  *  RETURNS:        Nothing
  54.  *
  55.  */
  56. static void DrawScreen(void)
  57. {
  58.     int  color = 1, x, y;
  59.     char *text = "GIF-file produced by GIFSAVE";
  60.  
  61.  
  62.     /*
  63.      *  Output some lines
  64.      */
  65.     setlinestyle(SOLID_LINE, 0, 3);
  66.     for (x = 10; x < getmaxx(); x += 20) {
  67.         setcolor(color);
  68.         line(x, 0, x, getmaxy());
  69.         if (++color > getmaxcolor())
  70.             color = 1;
  71.     }
  72.     for (y = 8; y < getmaxy(); y += 17) {
  73.         setcolor(color);
  74.         line(0, y, getmaxx(), y);
  75.         if (++color > getmaxcolor())
  76.             color = 1;
  77.     }
  78.  
  79.     /*
  80.      *  And then some text
  81.      */
  82.     setfillstyle(SOLID_FILL, DARKGRAY);
  83.     settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
  84.     bar(20, 10, textwidth(text) + 40, textheight(text) + 20);
  85.     setcolor(WHITE);
  86.     outtextxy(30, 10, text);
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93. /*-------------------------------------------------------------------------
  94.  *
  95.  *  NAME:           gpixel()
  96.  *
  97.  *  DESCRIPTION:    Callback function. Near version of getpixel()
  98.  *
  99.  *                  If this program is compiled with a model using
  100.  *                  far code, Borland's getpixel() can be used
  101.  *                  directly.
  102.  *
  103.  *  PARAMETERS:     As for getpixel()
  104.  *
  105.  *  RETURNS:        As for getpixel()
  106.  *
  107.  */
  108. static int gpixel(int x, int y)
  109. {
  110.     return getpixel(x, y);
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117. /*-------------------------------------------------------------------------
  118.  *
  119.  *  NAME:           GIF_DumpEga10()
  120.  *
  121.  *  DESCRIPTION:    Outputs a graphics screen to a GIF-file. The screen
  122.  *                  must be in the mode 0x10, EGA 640x350, 16 colors.
  123.  *
  124.  *                  No error checking is done! Probably not a very good
  125.  *                  example, then . . . :-)
  126.  *
  127.  *  PARAMETERS:     filename - Name of GIF-file
  128.  *
  129.  *  RETURNS:        Nothing
  130.  *
  131.  */
  132. static void GIF_DumpEga10(char *filename)
  133. {
  134.   #define WIDTH            640  /* 640 pixels across screen */
  135.   #define HEIGHT           350  /* 350 pixels down screen */
  136.   #define NUMCOLORS         16  /* Number of different colors */
  137.   #define BITS_PR_PRIM_COLOR 2  /* Two bits pr primary color */
  138.  
  139.     int q,                      /* Counter */
  140.         color,                  /* Temporary color value */
  141.         red[NUMCOLORS],         /* Red component for each color */
  142.         green[NUMCOLORS],       /* Green component for each color */
  143.         blue[NUMCOLORS];        /* Blue component for each color */
  144.     struct palettetype pal;
  145.  
  146.  
  147.     /*
  148.      *  Get the color palette, and extract the red, green and blue
  149.      *  components for each color. In the EGA palette, colors are
  150.      *  stored as bits in bytes:
  151.      *
  152.      *      00rgbRGB
  153.      *
  154.      *  where r is low intensity red, R is high intensity red, etc.
  155.      *  We shift the bits in place like
  156.      *
  157.      *      000000Rr
  158.      *
  159.      *  for each component
  160.      */
  161.     getpalette(&pal);
  162.     for (q = 0; q < NUMCOLORS; q++) {
  163.         color = pal.colors[q];
  164.         red[q]   = ((color & 4) >> 1) | ((color & 32) >> 5);
  165.         green[q] = ((color & 2) >> 0) | ((color & 16) >> 4);
  166.         blue[q]  = ((color & 1) << 1) | ((color & 8) >> 3);
  167.     }
  168.  
  169.     /*
  170.      *  Create and set up the GIF-file
  171.      */
  172.     GIF_Create(filename, WIDTH, HEIGHT, NUMCOLORS, BITS_PR_PRIM_COLOR);
  173.  
  174.     /*
  175.      *  Set each color according to the values extracted from
  176.      *  the palette
  177.      */
  178.     for (q = 0; q < NUMCOLORS; q++)
  179.         GIF_SetColor(q, red[q], green[q], blue[q]);
  180.  
  181.     /*
  182.      *  Store the entire screen as an image using the user defined
  183.      *  callback function gpixel() to get pixel values from the screen
  184.      */
  185.     GIF_CompressImage(0, 0, -1, -1, gpixel);
  186.  
  187.     /*
  188.      *  Finish it all and close the file
  189.      */
  190.     GIF_Close();
  191. }
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. /**************************************************************************
  203.  *                                                                        *
  204.  *                    P U B L I C    F U N C T I O N S                    *
  205.  *                                                                        *
  206.  **************************************************************************/
  207.  
  208.  
  209. int main(void)
  210. {
  211.     int gdr, gmd, errcode;
  212.  
  213.  
  214.     /*
  215.      *  Initiate graphics screen for EGA mode 0x10, 640x350x16
  216.      */
  217.     gdr = EGA;
  218.     gmd = EGAHI;
  219.     initgraph(&gdr, &gmd, "");
  220.     if ((errcode = graphresult()) != grOk) {
  221.         printf("Graphics error: %s\n", grapherrormsg(errcode));
  222.         exit(-1);
  223.     }
  224.  
  225.     /*
  226.      *  Put something on the screen
  227.      */
  228.     DrawScreen();
  229.  
  230.     /*
  231.      *  Dump the screen to a GIF-file
  232.      */
  233.     GIF_DumpEga10("EXAMPLE.GIF");
  234.  
  235.     /*
  236.      *  Return to text mode
  237.      */
  238.     closegraph();
  239.  
  240.     return 0;
  241. }
  242.